package com.yjx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjx.mapper.OwnerMapper;
import com.yjx.util.AssertUtil;
import com.yjx.vo.House;
import com.yjx.service.HouseService;
import com.yjx.mapper.HouseMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* @author 10657
* @description 针对表【house】的数据库操作Service实现
* @createDate 2023-05-11 15:55:14
*/
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House>
    implements HouseService{
    @Resource
    private HouseMapper houseMapper;
    @Resource
    private OwnerMapper ownerMapper;
    @Override
    public PageInfo<House> queryHouseAll(int page, int limit, String numbers) {
        PageHelper.startPage(page,limit);
        List<House> list=houseMapper.queryHouseAll(numbers);
        PageInfo<House> pageInfo=new PageInfo<>(list);
        return pageInfo;

    }

    @Override
    public List<House> findList() {
        return houseMapper.queryList();
    }

    @Override
    public boolean deleteByIds(Integer[] ids) {
        List<Integer> list = ownerMapper.queryOwnerHouse();
        List<Integer> delList = Arrays.asList(ids);
        System.out.println(list);
        System.out.println(delList);
        if(Collections.disjoint(list,delList)) {

            int count=houseMapper.deleteBatchIds(delList);
            int len = delList.size();
            return (count==len);
        }else{
            AssertUtil.isTrue(Collections.disjoint(list, delList), "所选楼号仍存在户主！");
            return false;

        }
    }

    @Override
    public boolean addHouse(House house) {
        int count =houseMapper.insert(house);
        return  (count>0);
    }

    @Override
    public boolean updateHouse(House house) {
        int count = houseMapper.updateById(house);
        return count>0;
    }
}




